<template>
  <a-drawer
    :title="title"
    width="70%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px' }"
    @close="handleCancel"
  >
    <a-skeleton :loading="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="!addable"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['userId']"
            :disabled="editable || showable"
          />
        </a-form-item>
        <a-form-item
          label="用户名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="editable || showable"
            v-decorator="['username', {rules: [{required: true, message: '请输入用户名'}]}]" />
        </a-form-item>
        <a-form-item
          label="部门"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-tree-select
            :disabled="showable"
            showSearch
            placeholder="选择部门"
            allowClear
            :treeData="treeData"
            treeDefaultExpandAll
            treeNodeFilterProp="title"
            v-decorator="['deptId', {rules: [{required: true, message: '请选择部门'}],initialValue: '1'}]"
          />
        </a-form-item>
        <a-form-item
          label="岗位"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <biz-select
            :disabled="showable"
            v-decorator="['postId', {rules: [{required: true, message: '选择岗位'}],initialValue: '1'}]"
            dicUrl="/sys/post/dropdown"
          >
          </biz-select>
        </a-form-item>
        <a-form-item
          label="状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            v-decorator="['status', {rules: [{required: true, message: '选择状态'}],initialValue: '1'}]"
            :options="statusTypeList"
          >
          </a-select>
        </a-form-item>
        <a-form-item
          label="角色"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            mode="multiple"
            v-decorator="['roleIdList', {rules: [{required: true, message: '选择状态'}]}]"
            :options="roleList"
          >
          </a-select>
        </a-form-item>
        <a-form-item
          label="手机号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['phone', {rules: [{required: true, message: '请输入手机号'}]}]" />
        </a-form-item>
      </a-form>
    </a-skeleton>

    <div
      :style="{
        position: 'absolute',
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e8e8e8',
        padding: '10px 16px',
        textAlign: 'left',
        left: 0,
        background: '#fff',
        borderRadius: '0 0 4px 4px',
      }"
    >
      <a-button type="primary" style="marginLeft: 8px" @click="handleOk">
        新建
      </a-button>
      <a-button type="primary" style="marginLeft: 8px" ghost @click="handleOk(true)">
        新建并继续
      </a-button>
      <a-button style="marginLeft: 8px" @click="handleCancel">
        取消
      </a-button>
    </div>
  </a-drawer>

</template>

<script>
import pick from 'lodash.pick'
import { getObj, addObj, putObj } from '@api/sys/user'
import { roleDropDown } from '@api/sys/role'
import { treeDataTranslate } from '@/utils/util'
import { fetchList } from '@api/sys/dept'
import { FormMixin } from '@/mixins/FormMixin'
import { statusTypeList } from '@/dict'

export default {
  name: 'UserAddOrUpdate',
  mixins: [FormMixin],
  data () {
    return {
      statusTypeList,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      treeData: [],
      roleList: [],
      postList: []
    }
  },
  methods: {
    // 获取用户信息
    edit (id, type) {
      this.visible = true
      roleDropDown().then(res => {
        this.roleList = res.data
      })
      fetchList().then(res => {
        this.treeData = treeDataTranslate(res.data, 'deptId', 'name')
      })
      const { form: { setFieldsValue } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data.sysUser
          record.roleIdList = res.data.roles
          record.deptId = String(record.deptId)
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['userId', 'username', 'phone', 'status', 'deptId', 'roleIdList']))
          })
        })
      }
    },
    // 结束保存用户信息
    handleOk (con = false) {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          try {
            if (this.type === 'add') {
              await addObj(values)
            } else if (this.type === 'edit') {
              await putObj(values)
            }
            if (con) {
              // this.form.resetFields()
              this.confirmLoading = false
              this.$emit('ok')
            } else {
              setTimeout(() => {
                this.confirmLoading = false
                this.$emit('ok')
                this.visible = false
              }, 1500)
            }
          } catch (e) {
            this.$message.error(e.msg)
            this.confirmLoading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
